package com.back.baseModule.controller;


import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.back.api.CommonResult;
import com.back.baseModule.entity.SysUser;
import com.back.baseModule.service.SysUserService;
import com.back.api.CommonPage;
import com.back.util.ExcelUtilOut;
import com.back.util.FileUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.*;

@Controller
@RequestMapping("/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult login(@RequestBody SysUser sysUser) {
        sysUser.setTel(sysUser.getUsername());
        SaTokenInfo saTokenInfo = sysUserService.login(sysUser);
        if (saTokenInfo == null) {
            return CommonResult.validateFailed("用户名或密码错误或账号已被禁用");
        }
        Map<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("token", saTokenInfo.getTokenValue());
        tokenMap.put("tokenHead", saTokenInfo.getTokenName());
        //返回登录信息
        SysUser adminUser = sysUserService.findByCondition(sysUser);
        tokenMap.put("user_id", adminUser.getId());
        tokenMap.put("user_name", adminUser.getName());
        tokenMap.put("user_tel", adminUser.getTel());
        tokenMap.put("sys_user_type", adminUser.getSysUserType());

        return CommonResult.success(tokenMap);
    }

    @RequestMapping(value = "/info", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult getAdminInfo() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String token = request.getHeader("Authorization");
        SysUser sysUser = new SysUser();
        sysUser.setId(StpUtil.getLoginId().toString());
        SysUser adminUser = sysUserService.findByCondition(sysUser);
        JSONObject resObj = JSONObject.parseObject(JSONObject.toJSONString(adminUser));
        resObj.put("roles", new String[]{"TEST"});
        resObj.put("menus", null);
        return CommonResult.success(resObj);
    }

    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult logout() {
        StpUtil.logout();
        return CommonResult.success(null);
    }

    @RequestMapping(value = "/isLogin", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult isLogin() {
        return CommonResult.success(StpUtil.isLogin());
    }

    //查询XXXxX列表
    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<Map<String, Object>>> getList(@RequestBody Map<String, Object> map) {
        List<Map<String, Object>> sysUserList = sysUserService.getList(map);
        return CommonResult.success(CommonPage.restPage(sysUserList));
    }

    @RequestMapping(value = "/findByCondition", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<SysUser> findByCondition(@RequestBody SysUser sysUser) {
        return CommonResult.success(sysUserService.findByCondition(sysUser));
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Map<String, Object>> insert(@RequestBody SysUser sysUser) {
        return CommonResult.success(sysUserService.save(sysUser));
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Map<String, Object>> delete(@RequestBody SysUser sysUser) {
        return CommonResult.success(sysUserService.delete(sysUser));
    }

    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Map<String, Object>> updateById(@RequestBody SysUser sysUser) {
        return CommonResult.success(sysUserService.updateById(sysUser));
    }

    /**
     * 导出excel
     */
    @RequestMapping(value = "/exportExcel", method = RequestMethod.GET)
    public ResponseEntity<byte[]> exportExcel(HttpServletResponse response,
                                              @RequestParam(value = "keyword", required = false) String keyword,
                                              @RequestParam(value = "tel", required = false) String tel,
                                              @RequestParam(value = "sort", required = false) String sort,
                                              @RequestParam(value = "create_time_s", required = false) String create_time_s,
                                              @RequestParam(value = "create_time_e", required = false) String create_time_e,
                                              @RequestParam(value = "sortType", required = false) String sortType,
                                              @RequestParam(value = "user_type", required = false) String user_type) throws Exception {
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("keyword", keyword);
        data.put("tel", tel);
        data.put("sort", sort);
        data.put("sortType", sortType);
        data.put("user_type", user_type);

        data.put("create_time_s", create_time_s);
        data.put("create_time_e", create_time_e);

        List<Map<String, Object>> studentList = sysUserService.getList(data);
        List<Map<String, String>> list = new ArrayList<>();
        if (studentList != null && studentList.size() > 0) {
            Map<String, String> titlec = null;

            String email = "";
            String tels = "";
            String name = "";
            String sex = "";
            String note = "";
            String unit = "";
            String id_card_num = "";
            String create_user_id = "";
            String create_time = "";
            String login_time = "";
            String status = "";
            String del_type = "";
            String sys_user_type = "";
            String open_id = "";
            String unit_nature = "";

            for (int i = 0, rowSize = studentList.size(); i < rowSize; i++) {
                titlec = new LinkedHashMap<>();

                email = "";
                if (null != studentList.get(i).get("email") && !studentList.get(i).get("email").toString().isEmpty()) {
                    email = studentList.get(i).get("email").toString();
                }
                tels = "";
                if (null != studentList.get(i).get("tel") && !studentList.get(i).get("tel").toString().isEmpty()) {
                    tels = studentList.get(i).get("tel").toString();
                }
                name = "";
                if (null != studentList.get(i).get("name") && !studentList.get(i).get("name").toString().isEmpty()) {
                    name = studentList.get(i).get("name").toString();
                }
                sex = "";
                if (null != studentList.get(i).get("sex") && !studentList.get(i).get("sex").toString().isEmpty()) {
                    sex = studentList.get(i).get("sex").toString();
                }
                note = "";
                if (null != studentList.get(i).get("note") && !studentList.get(i).get("note").toString().isEmpty()) {
                    note = studentList.get(i).get("note").toString();
                }
                unit = "";
                if (null != studentList.get(i).get("unit") && !studentList.get(i).get("unit").toString().isEmpty()) {
                    unit = studentList.get(i).get("unit").toString();
                }
                id_card_num = "";
                if (null != studentList.get(i).get("id_card_num") && !studentList.get(i).get("id_card_num").toString().isEmpty()) {
                    id_card_num = studentList.get(i).get("id_card_num").toString();
                }
                create_user_id = "";
                if (null != studentList.get(i).get("create_user_name") && !studentList.get(i).get("create_user_name").toString().isEmpty()) {
                    create_user_id = studentList.get(i).get("create_user_name").toString();
                }
                create_time = "";
                if (null != studentList.get(i).get("create_time") && !studentList.get(i).get("create_time").toString().isEmpty()) {
                    create_time = studentList.get(i).get("create_time").toString();
                }
                login_time = "";
                if (null != studentList.get(i).get("login_time") && !studentList.get(i).get("login_time").toString().isEmpty()) {
                    login_time = studentList.get(i).get("login_time").toString();
                }
                status = "";
                if (null != studentList.get(i).get("status") && !studentList.get(i).get("status").toString().isEmpty()) {
                    status = studentList.get(i).get("status").toString();
                }
                del_type = "";
                if (null != studentList.get(i).get("del_type") && !studentList.get(i).get("del_type").toString().isEmpty()) {
                    del_type = studentList.get(i).get("del_type").toString();
                }
                sys_user_type = "";
                if (null != studentList.get(i).get("sys_user_type") && !studentList.get(i).get("sys_user_type").toString().isEmpty()) {

                    if ("0".equals(studentList.get(i).get("sys_user_type").toString())) {
                        sys_user_type = "普通管理员";
                    } else if ("1".equals(studentList.get(i).get("sys_user_type").toString())) {
                        sys_user_type = "系统管理员";
                    } else if ("2".equals(studentList.get(i).get("sys_user_type").toString())) {
                        sys_user_type = "运管团队负责人";
                    } else if ("3".equals(studentList.get(i).get("sys_user_type").toString())) {
                        sys_user_type = "预约管理员";
                    } else if ("4".equals(studentList.get(i).get("sys_user_type").toString())) {
                        sys_user_type = "运管团队管理员";
                    } else if ("5".equals(studentList.get(i).get("sys_user_type").toString())) {
                        sys_user_type = "物业管理员";
                    } else if ("6".equals(studentList.get(i).get("sys_user_type").toString())) {
                        sys_user_type = "观展单位联络人";
                    } else if ("10".equals(studentList.get(i).get("sys_user_type").toString())) {
                        sys_user_type = "超级管理员";
                    }

                }
                open_id = "";
                if (null != studentList.get(i).get("open_id") && !studentList.get(i).get("open_id").toString().isEmpty()) {
                    open_id = studentList.get(i).get("open_id").toString();
                }
                unit_nature = "";
                if (null != studentList.get(i).get("unit_nature") && !studentList.get(i).get("unit_nature").toString().isEmpty()) {
                    unit_nature = studentList.get(i).get("unit_nature").toString();
                }


                titlec.put("email", email);
                titlec.put("tels", tels);
                titlec.put("name", name);
                titlec.put("sex", sex);
                titlec.put("note", note);
                titlec.put("unit", unit);
                titlec.put("id_card_num", id_card_num);
                titlec.put("create_user_id", create_user_id);
                titlec.put("create_time", create_time);
                titlec.put("login_time", login_time);
                titlec.put("status", status);
                titlec.put("del_type", del_type);
                titlec.put("sys_user_type", sys_user_type);
                titlec.put("open_id", open_id);
                titlec.put("unit_nature", unit_nature);

                list.add(titlec);
            }
        }

        Map<String, String> titles = new LinkedHashMap<>();
        titles.put("单位名称", "unit");
        titles.put("姓名", "name");
        titles.put("手机号码", "tels");
        titles.put("身份证", "id_card_num");
        titles.put("创建人", "create_user_id");
        titles.put("创建时间", "create_time");
        titles.put("类型", "sys_user_type");
        titles.put("备注", "note");
        String sheetname = "条件导出系统管理员";
        String path = ExcelUtilOut.outfile(response, titles, sheetname, list);
        File file = new File(path);
        return FileUtils.buildResponseEntity(file);
    }

}
